var provinces, cities, areas;

function sendAllProvinceAjax(path) {

	$.ajax({
		type : "get",
		url : path + '/jqueryui/jarea/queryAllProvinces.js',
		async : false,
		dataType : "json",
		success : function(data) {
			provinces = data.provinces;
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {

			alert(textStatus);
		}
	});

}
function sendAllCitiesAjax(path) {
	$.ajax({
		type : "get",
		url : path + '/jqueryui/jarea/queryCities.js',
		async : false,
		dataType : "json",
		success : function(data) {
			cities = data.cities;
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			alert(textStatus);
		}
	});
}
function sendAllCountiesAjax(path) {
	$.ajax({
		type : "get",
		url : path + "/jqueryui/jarea/queryAllAreas.js",
		async : false,
		dataType : "json",
		success : function(data) {
			areas = data.areas;
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			alert("网络loading慢");
		}
	});
}

function f_SelProvince(sel_province, sel_city, sel_district, path) {

	sendAllProvinceAjax(path);

	$.each(provinces, function(k, p) {
		var option = "<option value='" + p.provinceName + "'  id='"+p.id+"'>" + p.provinceName
				+ "</option>";
		$("#" + sel_province).append(option);
	});

	if (sel_city != null) {
		$("#" + sel_province).bind("change", function() {
			sel_province_change(sel_province, sel_city, sel_district, path);
		});
	}
	;

};

function sel_province_change(sel_province, sel_city, sel_district, path) {

	var selValue = $("#" + sel_province+"  option:selected").attr("id");
	

	$("#" + sel_city + " option:gt(0)").remove();

	if (sel_district != null) {
		$("#" + sel_district + " option:gt(0)").remove();
	}
	;

	sendAllCitiesAjax(path);

	$.each(cities,
			function(k, p) {
				if (p.provinceId == selValue) {
					var option = "<option value='"+p.name+"'  id='" + p.id + "'>" + p.name
							+ "</option>";

					//alert(option);
					$("#" + sel_city).append(option);
				}
			});

	if (sel_district != null) {
		$("#" + sel_city).bind("change", function() {
			sel_city_change(sel_province, sel_city, sel_district, path);
		});
	}
	;

};

function sel_city_change(sel_province, sel_city, sel_district, path) {
	var selValue = $("#" + sel_city +"  option:selected").attr("id");
	$("#" + sel_district + "option:gt(0)").remove();

	sendAllCountiesAjax(path);

	$.each(areas, function(k, p) {
		if (p.cityId == selValue) {
			var option = "<option value='" + p.areaName+"'  id='"+p.id + "'>" + p.areaName
					+ "</option>";
			$("#" + sel_district).append(option);
		}
	});
};